<?php

class Umanager extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('articlesModel');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('Jobs');
        $this->load->model('Company');

        $user = & Users::getInstance();
        if ($user->level < 1) {
            redirect('/');
        }
    }

    // Companies
    public function companies() {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        $search = $this->input->post('search');
        if (empty($search)) {
            $data['companies'] = $this->Company->getAll();
        } else {
            $data['companies'] = $this->Company->search($search);
        }

        $this->load->view('company-list.php', $data);
    }

    public function showCompany($cid) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        $data['company'] = $this->Company->getCompany($cid);

        $this->load->view('company.php', $data);
    }

    public function companyDelUser($uid) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        $this->Company->deleteUser($uid);

        redirect('/companies/');
    }

    public function editCompany($cid) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        if ($this->input->post('doedit')) {
            $this->Company->editCompany($cid, $this->input->post('name'));
        }

        $data['company'] = $this->Company->getCompany($cid);
        $data['formPost'] = '/companies/edit/' . $data['company']->id;
        $data['isEdit'] = true;

        $this->load->view('company-edit.php', $data);
    }

    public function newCompany() {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        if ($this->input->post('doedit')) {
            $this->Company->addCompany($this->input->post('name'));

            redirect('/companies');
        }

        $data['formPost'] = '/companies/new';
        $data['isEdit'] = false;

        $this->load->view('company-edit.php', $data);
    }

    // Users
    public function users() {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		$searchbox ="";
        $search = $this->input->post('search');
		if (isset($_REQUEST['searchbox'])){
			$searchbox = $_REQUEST['searchbox'];
		}
        if (empty($search)) {
            $data['users'] = $user->getAll();
        } else {
            $data['users'] = $user->searchCondition($search,$searchbox);
        }

        $this->load->view('users-list.php', $data);
    }

	function block($id) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
		$sql = "UPDATE `users` SET `Block`=1 WHERE `uid` =".$id;
        $this->db->query($sql);
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		$searchbox ="";
        $search = $this->input->post('search');
		if (isset($_REQUEST['searchbox'])){
			$searchbox = $_REQUEST['searchbox'];
		}
        if (empty($search)) {
            $data['users'] = $user->getAll();
        } else {
            $data['users'] = $user->searchCondition($search,$searchbox);
        }
		
        $this->load->view('users-list.php', $data);
    }
	
	function unblock($id) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
		$sql = "UPDATE `users` SET `Block`=0 WHERE `uid` =".$id;
        $this->db->query($sql);
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);
		$searchbox ="";
        $search = $this->input->post('search');
		if (isset($_REQUEST['searchbox'])){
			$searchbox = $_REQUEST['searchbox'];
		}
        if (empty($search)) {
            $data['users'] = $user->getAll();
        } else {
            $data['users'] = $user->searchCondition($search,$searchbox);
        }

        $this->load->view('users-list.php', $data);
    }
	
    public function loginasuser($uid) {
		$this->load->library('session');
        $user = & Users::getInstance();		
	
        $userinfo = $user->getUserInfo($uid);
		
		$loginOK = $user->login($userinfo->email, "isadminlogintouser", 0);		
        
        if ($loginOK) {			
           redirect('/');
        } else {
            redirect('/users');
        }
    }

    public function editUser($uid) {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;

        $data['iscurrentuser'] = false;
        if ($uid == $user->id) {
            $data['iscurrentuser'] = true;
        }
//                echo $uid;die;
//                echo "<pre>";print_r($user);die;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        if ($this->input->post('doedit')) {
            $user->adminEdit($uid, $this->input->post('email'), $this->input->post('password'), $this->input->post('confirmpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), $this->input->post('level'), $this->input->post('user_id'), $this->input->post('company'));
        }

        $data['uinfo'] = $user->getUserInfo($uid, false);
        $data['formPost'] = '/users/edit/' . $data['uinfo']->id;
        $data['isEdit'] = true;
        $data['companies'] = $this->Company->getAll();

        $this->load->view('user-edit.php', $data);
    }

    public function newUser() {
        $this->load->library('Menu');
        $menu = new Menu;
        $user = & Users::getInstance();
        $data = $menu->gen();
        $data['user'] = $user;
        $data['stats']['oncoming'] = $this->Jobs->getOncoming(-1, true);
        $data['stats']['quote'] = $this->Jobs->getNeedQuote(-1, true);

        if ($this->input->post('doedit')) {
            $user->adminAdd($this->input->post('email'), $this->input->post('password'), $this->input->post('confirmpassword'), $this->input->post('name'), $this->input->post('address'), $this->input->post('phone'), $this->input->post('level'), $this->input->post('user_id'), $this->input->post('company'));

            redirect('/users');
        }

        $data['formPost'] = '/users/new';
        $data['isEdit'] = false;
        $data['companies'] = $this->Company->getAll();

        $this->load->view('user-edit.php', $data);
    }

    public function delUser($id) {
        $user = & Users::getInstance();

        if ($id > 0 && $user->level == 3) {
            $data = array("owner_id" => $id);
            $this->db->delete('job_group', $data);

            $data = array("owner_id" => $id);
            $this->db->delete('jobs', $data);

            $data = array("uid" => $id);
            $this->db->delete('users', $data);
        }

        redirect('/users');
    }

    public function addJobOrder($id) {
        $user = & Users::getInstance();

        if ($id > 0 && $user->level > 0) {
            $info = array('jobUserId' => $id);
            $this->session->set_userdata($info);

            redirect('/request/trip-based');
        } else {
            redirect('/');
        }
    }

}

?>